package test {
	
	import com.effectiveui.frameworks.commando.CommandoInterpereter;
	import com.effectiveui.frameworks.commando.error.NoSuchCommandError;
	
	import flexunit.framework.TestCase;
	import flexunit.framework.TestSuite;

	public class CommandTests extends TestCase {
		
		public var commandExecuted:Boolean = false;
		public var noArgOptionFound:Boolean = false;
		public var testWord:String;
		
		public function CommandTests(testMethod:String) {
			super(testMethod);
		}
		
		public static function suite():TestSuite {
			var testSuite:TestSuite = new TestSuite();
			
			testSuite.addTest(new CommandTests("testExecuteSimpleCommand"));
			testSuite.addTest(new CommandTests("testExecuteSimpleCommandFails"));
			testSuite.addTest(new CommandTests("testParseTokens"));
			testSuite.addTest(new CommandTests("testExecuteCommandWithNoArgOptions"));
			testSuite.addTest(new CommandTests("testExecuteCommandWithOptions"));
			
			return testSuite;
		}
		
		public function testParseTokens():void {
			// parse tokens tokenizes a command line with the verb already parsed out
			var commandLine:String = "-a -n 'Tony Hillerson' --email tony-hillerson@effectiveui.com"
			var tokens:Array = CommandoInterpereter.getInstance().parseTokens(commandLine);
			assertEquals("there should be 5 tokens", 5, tokens.length);
			assertEquals("first token should be 'a'", '-a', tokens[0]);
			assertEquals("second token should be 'n'", '-n', tokens[1]);
			assertEquals("third token should be 'Tony Hillerson'", 'Tony Hillerson', tokens[2]);
			assertEquals("fourth token should be 'email'", '--email', tokens[3]);
			assertEquals("fifth token should be 'tony-hillerson@effectiveui.com'", 'tony-hillerson@effectiveui.com', tokens[4]);
		}
		
		public function testExecuteSimpleCommand():void {
			TestCommand.commandTest = this;
			assertFalse(commandExecuted);
			CommandoInterpereter.getInstance().execute("test");
			assertTrue("expected command to execute", commandExecuted);
		}
		
		public function testExecuteSimpleCommandFails():void {
			TestCommand.commandTest = this;
			var caughtError:Boolean = false;
			try {
				CommandoInterpereter.getInstance().execute("no-op")
			} catch (e:NoSuchCommandError) {
				caughtError = true;
				assertEquals("command no-op not found", e.message);
			}
			assertTrue("no-op is not a registered command", caughtError);
		}
		
		public function testExecuteCommandWithNoArgOptions():void {
			OptionTestCommand.commandTest = this;
			
			assertFalse(noArgOptionFound);
			CommandoInterpereter.getInstance().execute("option-test -n");
			assertTrue("expected command `option-test -n` to run", noArgOptionFound);
			
			noArgOptionFound = false;
			CommandoInterpereter.getInstance().execute("option-test -no-arg");
			assertTrue("expected command `option-test -no-arg` to run", noArgOptionFound);
			
			noArgOptionFound = false;
			CommandoInterpereter.getInstance().execute("option-test --no-arg");
			assertTrue("expected command `option-test --no-arg` to run", noArgOptionFound);
		}
		
		public function testExecuteCommandWithOptions():void {
			OptionTestCommand.commandTest = this;
			
			assertNull(testWord);
			CommandoInterpereter.getInstance().execute("option-test -w word");
			assertEquals("expected command `option-test -w word` to set test word to 'word'", 'word', testWord);
			
			testWord = '';
			CommandoInterpereter.getInstance().execute("option-test --test-word word");
			assertEquals("expected command `option-test --test-word word` to set test word to 'word'", 'word', testWord);
			
			testWord = '';
			CommandoInterpereter.getInstance().execute("option-test --test-word 'A Different Word'");
			assertEquals("expected command `option-test --test-word word` to set test word to 'A Different Word'", 'A Different Word', testWord);
		}
		
	}
}